package com.llxwz.controller;


import com.llxwz.dao.TagMapper;
import com.llxwz.entity.*;
import com.llxwz.service.*;
import com.llxwz.util.CommunityConstant;
import com.llxwz.util.CommunityUtil;
import com.llxwz.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
public class DiscussPostController implements CommunityConstant {

    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private UserService userService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private LikeService likeService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private TagService tagService;

    @PostMapping("/addPost")
    @ResponseBody
    public String addPost(String title,String content,Integer tag){
        User user = hostHolder.getUser();
        if (user == null){
            return CommunityUtil.getJsonString(403,"您还没有登陆!!");
        }

        DiscussPost discussPost = new DiscussPost();
        discussPost.setUserId(user.getId());
        discussPost.setTitle(title);
        discussPost.setContent(content);
        discussPost.setCreateTime(new Date());
        discussPost.setTagId(tag);
        discussPostService.addDiscussPost(discussPost);


        return CommunityUtil.getJsonString(0,"发布成功");

    }

    @GetMapping("/getPost/{postId}")
    public String getPost(@PathVariable("postId") int postId, Model model, Page page){
        //帖子详情
        DiscussPost post = discussPostService.findDiscussPostById(postId);
        model.addAttribute("post",post);
        //帖子的作者
        User user = userService.findById(post.getUserId());
        model.addAttribute("user",user);
        //该帖子点赞数量
        long likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_POST,postId);
        model.addAttribute("likeCount",likeCount);

        //当前用户是否对该帖子点赞状态
        int likeStatus = hostHolder.getUser()==null? 0 : likeService.findEntityLikeStatus(
                hostHolder.getUser().getId(),ENTITY_TYPE_POST,postId);
        model.addAttribute("likeStatus",likeStatus);

        //评论帖子分页信息
        page.setLimit(5);
        page.setPath("/getPost"+postId);
        page.setRows(post.getCommentCount());

        //评论帖子的列表
        List<Comment> commentList = commentService.findCommentsByEntityType(ENTITY_TYPE_POST,post.getId(),
                page.getCurrent(),page.getLimit());

        //封装评论的全部结果
        List<Map<String,Object>> commentVoList = new ArrayList<>();

        if (commentList!=null){
            //遍历帖子的每条评论
            for (Comment comment : commentList){
                //评论帖子的全部详情
                Map<String,Object> commentVoMap = new HashMap<>();
                //帖子的直接评论
                commentVoMap.put("comment",comment);
                //评论的作者
                commentVoMap.put("user",userService.findById(comment.getUserId()));


                //该评论点赞数量
                likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_COMMENT,comment.getId());
                commentVoMap.put("likeCount",likeCount);

                //当前用户是否对该评论点赞状态
                likeStatus = hostHolder.getUser()==null? 0 : likeService.findEntityLikeStatus(
                        hostHolder.getUser().getId(),ENTITY_TYPE_COMMENT,comment.getId());
                commentVoMap.put("likeStatus",likeStatus);


                //回复列表(回复帖子的直接评论的评论)
                List<Comment> replyList = commentService.findCommentsByEntityType(
                        ENTITY_TYPE_COMMENT,comment.getId(),1,Integer.MAX_VALUE
                );
                //回复的全部详情
                List<Map<String,Object>> replyVoList = new ArrayList<>();
                if(replyList != null){
                    for (Comment reply:replyList){
                        //回复的全部详情
                        Map<String,Object> replyVoMap = new HashMap<>();
                        //回复
                        replyVoMap.put("reply",reply);
                        //回复的作者
                        replyVoMap.put("user",userService.findById(reply.getUserId()));
                        //回复的 目标/用户
                        User target = reply.getTargetId()==0?null:userService.findById(reply.getTargetId());
                        replyVoMap.put("target",target);

                        //该评论点赞数量
                        likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_COMMENT,reply.getId());
                        replyVoMap.put("likeCount",likeCount);

                        //当前用户是否对该评论点赞状态
                        likeStatus = hostHolder.getUser()==null? 0 : likeService.findEntityLikeStatus(
                                hostHolder.getUser().getId(),ENTITY_TYPE_COMMENT,reply.getId());
                        replyVoMap.put("likeStatus",likeStatus);

                        replyVoList.add(replyVoMap);

                    }
                }

                commentVoMap.put("replys",replyVoList);

                //回复数量
                int replyCount = commentService.findCountByEntity(ENTITY_TYPE_COMMENT, comment.getId());
                commentVoMap.put("replyCount",replyCount);

                commentVoList.add(commentVoMap);

            }
        }

        model.addAttribute("comments",commentVoList);

        return "site/discuss-detail";
    }

    // 置顶
    @PostMapping("/discuss/top")
    @ResponseBody
    public String setTop(int id) {
        discussPostService.updateType(id, 1);

        return CommunityUtil.getJsonString(0);
    }

    //取消置顶
    @PostMapping("/discuss/unTop")
    @ResponseBody
    public String setUnTop(int id) {
        discussPostService.updateType(id, 0);

        return CommunityUtil.getJsonString(0);
    }

    // 加精
    @PostMapping("/discuss/wonderful")
    @ResponseBody
    public String setWonderful(int id) {
        discussPostService.updateStatus(id, 1);

        return CommunityUtil.getJsonString(0);
    }

    //取消加精
    @PostMapping("/discuss/unWonderful")
    @ResponseBody
    public String setUnWonderful(int id) {
        discussPostService.updateStatus(id, 0);

        return CommunityUtil.getJsonString(0);
    }

    // 删除
    @PostMapping("/discuss/delete")
    @ResponseBody
    public String setDelete(int id) {
        discussPostService.updateStatus(id, 2);

        return CommunityUtil.getJsonString(0);
    }

    @GetMapping("/getPostList/{title}/{type}/{status}/{tagId}")
    public String getPostList(@PathVariable("title")String title,
                              @PathVariable("type")int type,
                              @PathVariable("status")int status,
                              @PathVariable("tagId")int tagId,
                              Model model, Page page){

        page.setRows(discussPostService.getPostRows(title,type,status,tagId));
        page.setPath("/getPostList/"+title+"/"+type+"/"+status+"/"+tagId);

        List<Tag> tags = tagService.findTags(ENTITY_TYPE_POST,0);
        model.addAttribute("tags",tags);

        List<DiscussPost> posts = discussPostService.getPostList(title, type, status, tagId, page.getCurrent(), page.getLimit());

        List<Map<String,Object>> result = new ArrayList<>();

        if(posts!=null){
            for (DiscussPost post:posts){
                Map<String,Object> map = new HashMap<>();
                map.put("post",post);
                User user = userService.findById(post.getUserId());
                map.put("user",user);
                Tag tag = tagService.findTagById(post.getTagId());
                map.put("tag",tag);

                result.add(map);
            }
        }

        model.addAttribute("result",result);
        return "site/admin/postList";
    }

    @PostMapping("/setPost")
    public String setPost(int id,int type,int status,int tag){
        discussPostService.updateTag(id,tag);
        discussPostService.updateType(id,type);
        discussPostService.updateStatus(id,status);
        return "redirect:/getPostList/null/-1/-1/-1";
    }

    //禁帖
    @PostMapping("/banPost")
    public String ban(int id) {
        discussPostService.updateStatus(id,2);
        return "redirect:/getPostList/null/-1/-1/-1";
    }

    //解封
    @PostMapping("/unbanPost")
    public String unban(int id) {
        discussPostService.updateStatus(id,0);
        return "redirect:/getPostList/null/-1/-1/-1";
    }

}
